<?php

/**
 * Validate different type of info
 * @author ntourne
 * @date 23/05/2011
 */

class Validation {

	/**
	 * Return true if the $var is empty (null or "")
	 * @param text
	 * @return boolean
	 */
	public static function isEmptyString($text) {
		return (!isset($text) || $text == null || trim($text) == "");
	}
	
	
	/**
	 * Return true if the $array is empty (null or 0 elements)
	 * @param Array $array
	 * @return boolean
	 */
	public static function isEmptyArray($array) {
		return (!isset($array) || $array == null || count($array) == 0);
	}
	
	
	/**
	 * Validate if $email has email format
	 * @param String $email
	 * @return boolean
	 */
    public static function isEmail($email) {
        if (preg_match('/\s/',$email) !== 0)
            return false;
        $pattern = "/\A[a-z0-9_+\-]+(\.[a-z0-9_+\-]+)*@([a-z0-9]+([a-z0-9]+|\.[a-z0-9]+|\-[a-z0-9]+)*\.(aero|arpa|asia|biz|cat|com|coop|edu|gov|info|int|jobs|mil|mobi|museum|name|net|org|pro|tel|travel|[a-z]{2})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?\z/i";
        $result = preg_match($pattern, $email);
        if ( $result === 0 || $result === false )
            return false;
        return true;
    }


    /**
     * Validate if string length is less than min value
     * @param boolean $text
     */
    public static function isLessMaxLengthString($text, $length) {
    	if (!Validation::isEmptyString($text)) {
    		return (strlen($text) <= $length);
    	}
    	return true; 
    }
    
    
    /**
     * Validate if both strings matches
     * @param String $text1
     * @param String $text2
     * @return true if both strings matches
     */
	public static function isStringMatch($text1, $text2) {
		return (trim($text1) == trim($text2));
	}

	
	/**
	 * Validate if the value is an integer (ej. 10 and '10' are both integers)
	 * @param string/int $value
	 * @return true if $value is a number
	 */
	public static function isInteger($value) {
		return is_numeric($value);
	}
	
	
	/**
	 * Validate if a User exists with the param's $username
	 * @param string $username
	 * @return true if the User doesnt' exist with the param's $username
	 */
	public static function isUsernameExists($username) {
		$user = new User();
		$user->username = $username;
		return $user->getUserByUsername();
	}
	

	/**
	 * Validate if a User exists with the param's $email
	 * @param string $email
	 * @return true if the User doesnt' exist with the param's $email
	 */
	public static function isEmailExists($email) {
		$user = new User();
		$user->email = $email;
		return $user->getUserByEmail();
	}
	
}

?>